package net.sourceforge.ehep.gui;

import net.sourceforge.ehep.core.FileManager;
import net.sourceforge.ehep.core.bufferManager;
import net.sourceforge.ehep.gui.HexEditorControl.AppendDataCommand;

import org.eclipse.swt.widgets.Composite;

/**
 * @author Sophia Ghins
 * Enhancement in project by @author randallco@users.sourceforge.net
 */
public class WatermarkEditControl {
	private final bufferManager bufManager;
	private int appValue;
	private String strVal = "watermark.char";
	private String strVal1 = "watermark.sign";
	
	/**
	 * Constructor
	 * @param parent
	 */
	public WatermarkEditControl(){
		bufManager = new bufferManager();
		//appValue = 67;
		appValue = 83;
	}
	
	/**
	 * Created by: Sophia Ghins
	 * set value to append
	 * Date: 05/17/09
	 */
	public void setValue(int val){
		this.appValue = val;
	}
	
	/**
	 * Created by: Sophia Ghins
	 * get the value
	 * Date: 05/17/09
	 */
	public int getValue(){
		return this.appValue;
	}
	
	/**
	 * Created by: Sophia Ghins
	 * get the bufferManager object
	 * Date: 05/20/09
	 */
	public bufferManager getBufManager(){
		return bufManager;
	}
	
	
//	/**
//	 * Created by: Sophia Ghins
//	 * method to add watermark (signature)to the current open file
//	 * Date: 05/13/09
//	 */
//	public void addWatermark(HexEditorControl editor){
//		int size, row, col;
//		boolean refreshview;
//		
//		row = editor.getHexTable().getItemCount();
//		col = editor.getHexTable().getItem(row-1).getItemSize();
//		size = 1;
//		refreshview = true;
//		
//		editor.appendData(size, appValue, row - 1, col, refreshview);
//	}

	/**
	 * Created by: Sophia Ghins
	 * method to add watermark (signature)to the current open file
	 * Date: 05/13/09
	 */
	public int addWatermark(HexEditorControl editor){
		int bufferIndex = 0;
		if(bufManager.getCurrFileBufSize() > 0){
			//bufManager.modifyBufInsCharEnd(strVal);
			if(bufManager.modifyBufInsSignEnd(strVal1) == -2){
				return -2;
			}
//			editor.getHexTable().setbufOverwrite(true);
			//return editor.getHexTable().overwriteBufData(bufferIndex, FileManager.buffer, bufManager.getCurrFileBufSize());
			return editor.getHexTable().overwriteBufDataInTable(FileManager.buffer);
		}else
			return -1;
	}
	
//	/**
//	 * Created by: Sophia Ghins
//	 * method to remove watermark (signature)from the current open file
//	 * (currently just removes the last cell from the table)
//	 * Date: 05/13/09
//	 */
//	public void deleteWatermark(HexEditorControl editor){
//		int size, row, col;
//		boolean refreshview, verify;
//		
//		row = editor.getHexTable().getItemCount();
//		col = editor.getHexTable().getItem(row-1).getItemSize();
//		size = 1;
//		refreshview = true;
//		
//		//if(verifyWatermark()){
//			editor.delete(row - 1, col - 1, size);
//		//}else{
//			//sorry no watermark signature found
//		//}
//	}
	
//	/**
//	 * Created by: Sophia Ghins
//	 * method to verify that a file has been watermarked (already has signature)
//	 * (currenty verifying that the last cell in the hex table is equal to ASCII value 'C')
//	 * Date: 05/13/09
//	 */
//	public boolean verifyWatermark(HexEditorControl editor){
//		int row, col;
//		String data;
//		boolean chk;
//				
//		data = "C";
//		row = editor.getHexTable().getItemCount();
//		col = editor.getHexTable().getItem(row-1).getItemSize();
//		chk = editor.getHexTable().verifyData(data, row - 1);
////		TODO: compare the value of this item with value while debugging		
//		if(chk){
//			System.out.println("Success");
//			return true;
//		}
//		return false;
//	}	
	
	/**
	 * method to verify that a file has been watermarked (already has signature present)
	 * @author Sophia Ghins
	 * Date: 06/21/09
	 */
	public boolean verifyWatermark(){
		//if(bufManager.verifyBufInsCharEnd(strVal)){
		if(bufManager.verifyBufInsSignEnd(strVal1)){	
			return true;
		}
		return false;
	}	

	/**
	 * method to remove watermark (signature)from the current open file
	 * @author Sophia Ghins
	 * Date: 06/21/09
	 */
	public int deleteWatermark(HexEditorControl editor){
		int bufferIndex = 0;
		//if(bufManager.removeBufCharEnd(strVal)){
		if(bufManager.removeBufSignEnd(strVal1)){
//			editor.getHexTable().setbufOverwrite(true);
			//return editor.getHexTable().overwriteBufData(bufferIndex, FileManager.buffer, bufManager.getCurrFileBufSize());
			return editor.getHexTable().overwriteBufDataInTable(FileManager.buffer);
		}
		return -1;
	}
	
	
	
}
